package yxy.game.pm2.active.command.sevenlogin;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actsevenreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_七日登陆;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActSevenLoginInfoRequest;
import yxy.game.pm2.message.PackageMessage;

import java.util.ArrayList;
import java.util.Map;

@MessageType(proto = Command.ActSevenLoginInfo, classtype = ActiveMessage.ActSevenLoginInfoRequest.class)
public class CMD_Active_七日登陆_Info extends Command<ActSevenLoginInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_七日登陆_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActSevenLoginInfoRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActSevenLoginInfoResponse.Builder response = ActiveMessage.ActSevenLoginInfoResponse.newBuilder();
		
		ActiveModule_七日登陆 am = ActiveModule_七日登陆.get(pcode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		response.setLogindays(am.getLogindays());
		
		Map<String, actsevenreward> sevenrewards = ActDataCenter.getAllData(actsevenreward.class, pcode);
		for(String key : sevenrewards.keySet()) {
			ActiveMessage.SevenLogin.Builder sl = ActiveMessage.SevenLogin.newBuilder();
			actsevenreward sevenreward = sevenrewards.get(key);
			int days = sevenreward.getId();
			sl.setDays(days);
			ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(sevenreward.getRewardtype(), sevenreward.getRewardid(), sevenreward.getRewardnum());
			for(Currency cur : curs) {
				PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();
				item.setType(cur.getType().getNumber());
				item.setDataId(cur.getDataId());
				item.setNumber(cur.getNumber());
				sl.addRewards(item.build());
			}
			
			PackageMessage.Item.Builder displayitem = PackageMessage.Item.newBuilder();
			displayitem.setType(sevenreward.getDisplaytype());
			displayitem.setDataId(sevenreward.getDisplayid());
			displayitem.setNumber(sevenreward.getDisplaynum());
			sl.setDisplay(displayitem.build());
			
			sl.setCanget(am.cangainreward(days));
			response.addSevenlogininfos(sl.build());
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActSevenLoginInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
